package com.eagledata.project.business.purchaseInfo.controller;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.eagledata.framework.config.EagleDataConfig;
import com.eagledata.framework.web.controller.BaseController;
import com.eagledata.framework.web.domain.Message;
import com.eagledata.framework.web.page.TableDataInfo;
import com.eagledata.project.business.image.domain.Image;
import com.eagledata.project.business.image.service.IImageService;
import com.eagledata.project.business.purchaseInfo.domain.PurchaseInfo;
import com.eagledata.project.business.purchaseInfo.service.IPurchaseInfoService;

/**
 * 置业 信息操作处理
 * 
 * @author eagledata
 * @date 2018-06-29
 */
@Controller
@RequestMapping("/business/purchaseInfo")
public class PurchaseInfoController extends BaseController {
    private String prefix = "business/purchaseInfo";
	
	@Autowired
	private IPurchaseInfoService purchaseInfoService;
	@Autowired
	private IImageService imageService;
	
	@RequestMapping()
	@RequiresPermissions("business:purchaseInfo:view")
	public String purchaseInfo() {
	    return prefix + "/purchaseInfo";
	}
	
	/**
	 * 查询置业列表
	 */
	@RequiresPermissions("business:purchaseInfo:list")
	@RequestMapping("/list")
	@ResponseBody
	public TableDataInfo list(PurchaseInfo purchaseInfo) {
		startPage();
        List<PurchaseInfo> list = purchaseInfoService.selectPurchaseInfoList(purchaseInfo);
		return getDataTable(list);
	}
	
	/**
	 * 新增置业
	 */
	@RequiresPermissions("business:purchaseInfo:add")
	@RequestMapping("/add")
	public String add(Model model) {
		//获取对应图片
		List<Image> datas = imageService.selectImageList(new Image());
		List<Image> result = new ArrayList<>();
		for (Image record : datas) {
			String imageUrl = StringUtils.join(EagleDataConfig.getBaseImgUrl(), record.getUrl());
			record.setUrl(imageUrl);
			result.add(record);
		}
		model.addAttribute("images", result);
	    return prefix + "/add";
	}

	/**
	 * 修改置业
	 */
	@RequiresPermissions("business:purchaseInfo:edit")
	@RequestMapping("/edit/{id}")
	public String edit(@PathVariable("id") Integer id, Model model) {
		PurchaseInfo purchaseInfo = purchaseInfoService.selectPurchaseInfoById(id);
		model.addAttribute("purchaseInfo", purchaseInfo);
	    return prefix + "/edit";
	}
	
	/**
	 * 保存置业
	 */
	@RequiresPermissions("business:purchaseInfo:save")
	@RequestMapping("/save")
	@ResponseBody
	public Message save(PurchaseInfo purchaseInfo) {
		if (purchaseInfoService.savePurchaseInfo(purchaseInfo) > 0)
		{
			return Message.success();
		}
		return Message.error();
	}
	
	/**
	 * 删除置业
	 */
	@RequiresPermissions("business:purchaseInfo:remove")
	@RequestMapping( "/remove/{id}")
	@ResponseBody
	public Message remove(@PathVariable("id") Integer id) {
		if (purchaseInfoService.deletePurchaseInfoById(id) > 0)
		{
		    return Message.success();
		}
		return Message.error();
	}
	
	/**
	 * 批量删除置业
	 */
	@RequiresPermissions("business:purchaseInfo:batchRemove")
	@RequestMapping( "/batchRemove")
	@ResponseBody
	public Message remove(@RequestParam("ids[]") Integer[] ids) {
		int rows = purchaseInfoService.batchDeletePurchaseInfo(ids);
		if (rows > 0)
        {
            return Message.success();
        }
        return Message.error();
	}
}
